savedatastate = -100

datapack = pge.zip.open("bin/data.bin")
fontdata = datapack:read("FONT.TTF")
font = pge.font.loadmemory(fontdata:data(),12,PGE_VRAM)
smallfont = pge.font.loadmemory(fontdata:data(),7,PGE_VRAM)
medfont = pge.font.loadmemory(fontdata:data(),10,PGE_VRAM)
texdata = datapack:read("s.png")
tex = pge.texture.loadmemory(texdata:data(),PGE_VRAM)
texdata = datapack:read("sq.png")
squ = pge.texture.loadmemory(texdata:data(),PGE_VRAM)
texdata = datapack:read("arrow.png")
arr = pge.texture.loadmemory(texdata:data(),PGE_VRAM)
texdata = datapack:read("wait.png")
wait = pge.texture.loadmemory(texdata:data(),PGE_VRAM)
texdata = datapack:read("lifeind.png")
lifeind = pge.texture.loadmemory(texdata:data(),PGE_VRAM)
texdata = datapack:read("manaind.png")
manaind = pge.texture.loadmemory(texdata:data(),PGE_VRAM)
texdata = nil
fontdata = nil

mnstd0=datapack:read("manstand0.png")
mnstd1=datapack:read("manstand1.png")
mnstd2=datapack:read("manstand2.png")
mnstd3=datapack:read("manstand3.png")
mnwlk0=datapack:read("manwalk0.png")
mnwlk1=datapack:read("manwalk1.png")

man = {
	pge.texture.loadmemory(mnstd0:data(),PGE_VRAM),
	pge.texture.loadmemory(mnstd1:data(),PGE_VRAM),
	pge.texture.loadmemory(mnstd2:data(),PGE_VRAM),
	pge.texture.loadmemory(mnstd3:data(),PGE_VRAM),
	pge.texture.loadmemory(mnwlk0:data(),PGE_VRAM),
	pge.texture.loadmemory(mnwlk1:data(),PGE_VRAM)
}

mnstd0=nil
mnstd1=nil
mnstd2=nil
mnstd3=nil
mnwlk0=nil
mnwlk1=nil

wear = {}

tmp = datapack:read("pp.png")
wear[1] = pge.texture.loadmemory(tmp:data(),PGE_VRAM)

tmp = datapack:read("emptybar.png")
emptybar = pge.texture.loadmemory(tmp:data(),PGE_VRAM)
tmp = datapack:read("fullbar.png")
fullbar = pge.texture.loadmemory(tmp:data(),PGE_VRAM)

tmp1 = datapack:read("life.png")
tmp2 = datapack:read("mana.png")
itemtex = {
	pge.texture.loadmemory(tmp1:data(),PGE_VRAM),
	pge.texture.loadmemory(tmp2:data(),PGE_VRAM)
}

datapack:close()

wcount = 5
bcount = 0
bway = true
sway = true
scount = 1
swait = 240
cmode = 0

triggeringitem = -1

mask2 = 0
items = {}
event = {}
quest = {set={{},{}}}
obj = {}

inventory = {{},{},{},{},{},{},{},{},{},{},{},{}}
for i = 1, 12 do
	for j = 1, 9 do
		inventory[i][j] = 0
	end
end

invcount = {{},{},{},{},{},{},{},{},{},{},{},{}}
for i = 1, 12 do
	for j = 1, 9 do
		invcount[i][j] = 1
	end
end

amitems = {"ASAKA","HAZISA","RPOROBNORSI","PQFEMFS\\","PQIHCAS] ETVA"}

itemsinfo = {
	{both=true,action=true,name="WFLFBN\\J NAPISOK",heal=50},
	{both=true,action=true,name="HFL]F MAN\\",mana=50}
}

unitsinfo = {
	{health=80,armor=20},
	{health=50,mana=50,armor=5}
}

cmenuitems={"INCFNSAQ]","OXKI TMFNIJ","RPOROBNORSI","KCFRS\\"}
pmenuitems={"ROVQANIS]","C\\JSI C MFN_","C\\JSI IH IDQ\\"}

messages = {"","","","",""}
messalpha = {0,0,0,0,0}

white = lua.createcolor(255,255,255)
black = lua.createcolor(0,0,0)
specialcolor = lua.createcolor(0,0,0,0)

alphan = false
alpha = 1

sqa = 0

invm = 0

health = 100
mana = 100

healthmax = 300
manamax = 300

h2 = health
m2 = mana

function loadlevel(name)
	local mapf = pge.file.open(name,PGE_FILE_RDONLY)
	local tmp = ""
	local s = ""
	while not (mapf:tell() >= mapf:size()) do
		tmp = mapf:read(1)
		if tmp=="^"
		then
			break
		else
			s = s..tmp
		end
	end

	local s3 = mapf:read(mapf:size()-mapf:tell())
	--while not (mapf:tell() >= mapf:size()) do
		--s3 = s3..mapf:read(1)
	--end
	mapf:close()
	local pat = pge.file.open("module/pattern1.lua",PGE_FILE_RDONLY)
	local s2 = pat:read(pat:size())
	pat:close()
	pat = pge.file.open("module/pattern2.lua",PGE_FILE_RDONLY)
	local s4 = pat:read(2048)
	local s5 = pat:read(pat:size()-2049)
	pat:close()

	local genf = pge.file.open("temp/gen.lua",PGE_FILE_WRONLY+PGE_FILE_CREATE+PGE_FILE_TRUNC)

	genf:write(s)
	genf:write(s2)
	genf:write(s3)
	genf:write(s4)
	genf:write(s5)

	genf:close()
	local script = assert(loadfile("temp/gen.lua"))
	if script then
		script()
		script = nil
		collectgarbage("collect")
	end
end

function calculateAll()
	vertices = {}
	local i = 1
	if camera >= 0 then i = pge.math.floor(camera/50)+1 end
	local m = #earth
	if i+11<#earth then m = i+11 end
	local i2 = 1
	while i <= m do
		vertices[i2] = {theme_earth, i*50-50-camera, 273-earth[i], 0}
		vertices[i2+1] = {theme_earth, i*50-50-camera, 273, 0}
		i = i+1
		i2 = i2+2
	end
	n = pge.math.floor(x/50)
	y = ((x-50*n)*(earth[n+2]-earth[n+1]))/50+earth[n+1]
	a = 0.6 * (earth[n+1] - earth[n+2])/50
end

function calcY(myx)
	n2 = pge.math.floor(myx/50)
	return ((myx-50*n2)*(earth[n2+2]-earth[n2+1]))/50+earth[n2+1]
end

function initLoad()
	savedatadialog = true
	pge.utils.loadinit(10,"BUGZ00000")
end

function initSave()
	savedatadialog = true
	pge.utils.saveinit("DATA","HBVR00000","Our Game","Saved Data","Contains some data")
end

function item()
	for i = 1, #items do
		if (pge.math.abs(items[i].x-x)<8) and (items[i].type>0)
		then
			triggeringitem=i
			return true
		end
	end
end

function drawobjs()
	for i = 1, #obj do
		objtex[obj[i].type]:activate()
		objtex[obj[i].type]:draw(obj[i].x-camera,obj[i].y)
	end
end

function drawitems()
	for i = 1, #items do
		if items[i].type>0 then
			itemtex[items[i].type]:activate()
			itemtex[items[i].type]:draw(items[i].x-camera-8,256-calcY(items[i].x))
		end
	end
end

function showmessage(str)
	i = 1
	while (i<=5) and (messalpha[i]>0) do
		i=i+1
	end
	if i==6
	then
		for i = 1, 4 do
			messages[i] = messages[i+1]
			messalpha[i] = messalpha[i+1]
		end
		i = 5
	end
	messages[i] = str
	messalpha[i] = 500
end

-- event handlers

function event.onInit()
	return levelstate.start
end

function event.onHeroEnteredZone(x1,x2)
	return (x>=x1) and (x<=x2) and ((prevx<x1) or (prevx>x2))
end

function event.onHeroLeavedZone(x1,x2)
	return ((x<x1) or (x>x2)) and (prevx>=x1) and (prevx<=x2)
end

function event.onItemPickedUp()
	return ipu
end

-- quests functions

function quest.create(group,title,desc)
	local num = #quest.set[group]+1
	quest.set[group][num] = {}
	quest.set[group][num].title = title
	quest.set[group][num].desc = desc
	quest.set[group][num].mode = 0
	return num
end

function quest.mark(num,mode)
	quest.set[group][num].mode = mode
end

nextlevel = "main.lua"

while pge.running() do
	camera = 0
	camera_follow = true
	way = 1
	mask = 255
	blackmask = false
	levelstate = {
		start = true
	}
	frame = 0
	lockcm = false
	invx=1
	invy=1
	items = {}
	hmbaralpha = 0
	hmbarwait = 200
	loadlevel("module/"..nextlevel)
end
